<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "schoolinfo".
 *
 * @property integer $id
 * @property string $stu_id
 * @property string $enroll_time
 * @property integer $stu_type
 * @property string $major_name
 * @property string $class_name
 * @property integer $status
 * 
 */
class SchoolinfoModel extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'schoolinfo';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['stu_id'], 'required'],
        		[['stu_id'], 'unique'],
            [['stu_type', 'status'], 'integer'],
            [['stu_id','name'], 'string', 'max' => 20],
            [['enroll_time', 'major_name', 'class_name'], 'string', 'max' => 50],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'stu_id' => '学号',
            'enroll_time' => '入学时间',
            'stu_type' => '学生类别',
            'major_name' => '专业名称',
            'class_name' => '班级名称',
            'status' => '学籍状态',
            'name'=>'姓名',
        ];
    }


    /**
     * 保存导入excel文件内容
     * @param $data  传入文件内容的数组
     * @return bool  保存成功返回true
     *
     */
    public static function SaveImport($data){

        try{


            foreach ($data as $value){
                $person=new SchoolinfoModel();
                $person->stu_id=$value['学号'].'';//学号要转化为字符型才能通过验证
                $person->name=$value['姓名'].'';
                $person->enroll_time=$value['入学时间'].'';
                $person-> stu_type=(int)$value['学生类别'];
                $person->major_name=$value['专业名称'].'';
                $person->class_name=$value['班级名称'].'';
                $person->status=(int)$value['学籍状态'];

                $person->save();
            }
            return true;

        }catch (\Exception $e){
            throw $e;
        }

    }


}
